package com.junxonline.plat.controller.house;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: HouseController
 * @Description: 房产控制器
 * @author JunX
 */

@RestController
@RequestMapping(value = "/admin/house")
public class HouseController extends BaseController {

    @Autowired
    private HouseService houseService;

    /**
     * 获取房产数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getHouse")
    public ResultVO getHouse(@RequestBody QueryHouseVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(houseService.getHouse(vo));
    }

    /**
     * 筛选查询房产数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryHouse")
    public PageResultVO queryHouse(PageVO page, QueryHouseVO vo) {
        return getReturnSuccess(houseService.queryHouse(page, vo));
    }


    /**
     * 添加房产
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addHouse")
    public ResultVO addHouse(@RequestBody HouseVO vo) {

        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = houseService.addHouse(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改房产
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editHouse")
    public ResultVO editHouse(@RequestBody HouseVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getHouseId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = houseService.editHouse(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除房产
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delHouse")
    public ResultVO delHouse(@RequestBody HouseIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] houseIdsArray = vo.getHouseIds();

        boolean successMenu = houseService.delHouse(houseIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 获取住户数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getHousehold")
    public ResultVO getHousehold(@RequestBody QueryHouseholdVO vo) {

        if (CommonUtils.isEmpty(vo.getHouseholdName())) {
            return getReturnSuccess();
        }

        return getReturnSuccess(houseService.getHousehold(vo));
    }

    /**
     * 获取户型数据
     * @return
     */
    @RequestMapping(value = "/getFloorPlan")
    public ResultVO getFloorPlan() {
        return getReturnSuccess(houseService.getFloorPlan());
    }

}


